let app = getApp();
Page({
  data: {
    infor: [],
    firstRow: 0,
    userName:'',
    is_show_time:1,
    is_show:0,
    
  },
  onLoad: function () {
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    if (uid){
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Directseller&met=index&typ=json',
        data: { k: key, u: uid },
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {

          if (res.data.status == 200) {
      
            that.setData({
              infor: res.data.data.items,
              is_show_time: 0,
              k:key,
              uid:uid
            })

          }
        }
      })
    }
    
  },
  //申请分销
  apply: function (e) {
    var that = this
    var shop_id = e.currentTarget.dataset.shop_id
    
    wx.showModal({
      title: '',
      content: '您确定要申请吗?',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Directseller&met=addDirectseller&typ=json',
            data: { k: that.data.k, u: that.data.uid, shop_id: shop_id },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              
              if (res.data.status == 200) {
                that.onLoad()
              }
            }
          })
        }
      }
    })
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    var that = this
    if (that.data.is_show == 1) {
      return false
    }
    that.setData({
      firstRow: that.data.firstRow + 10
    })
    if (that.data.userName){
      var data = { k: that.data.k, u: that.data.uid, firstRow: that.data.firstRow, userName: userName }
    }else{
      var data = { k: that.data.k, u: that.data.uid, firstRow: that.data.firstRow }
    }
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Directseller&met=index&typ=json',
      header: {
        'content-type': 'application/json'
      },
      data:data,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        var infor = that.data.infor
        if (res.data.data.items.length > 0) {

          for (var z in res.data.data.items) {
            infor.push(res.data.data.items[z])
          }
          that.setData({
            infor: infor,
            is_show_time: 0,
            is_show: 0

          })
        } else {
          that.setData({
            is_show: 1,
            is_show_time: 0

          })
        }

      }
    })
  },
  bindhref: function () {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {

        if (e.scrollTop + res.windowHeight > res.windowHeight) {

          that.setData({
            show: 1
          })
        } else {

          that.setData({

            show: 0
          })
        }
      },
    })
  },
})